#include "EnvironmentManager.h"
#include "HeightMapParser.h"

template<> GameGraphics::EnvironmentManager* Ogre::Singleton<GameGraphics::EnvironmentManager>::msSingleton = 0;

namespace GameGraphics
{


EnvironmentManager::EnvironmentManager(Ogre::SceneManager* iSceneManager) 
	:	mSceneManager(iSceneManager)
{
	// Inizializza il quadtree della scena (sostanzialmente crea il quadtree e calcola l'errore di proiezione)
	mQuadtree = new QuadTree<QTreeNodeData>(1024, 32, Ogre::Vector2(0.0f, 0.0f));

	Ogre::Camera* camera = iSceneManager->getCamera("Camera");
	const float fov = camera->getFOVy().valueRadians();
	const size_t screenHeight = camera->getViewport()->getActualHeight();

	mQuadtree->PostOrderTraversal(HMapProjectionBasedErrorParser<QTreeNodeData>(static_cast<size_t>(mQuadtree->GetWorldResolution()),
		static_cast<size_t>(mQuadtree->GetMinCellResolution()), "Heightmap.dds", 1000.0f, 20.0f, fov, screenHeight, 6.0f));

	mTerrainManager = new TerrainManager(mSceneManager, mQuadtree);
}

EnvironmentManager::~EnvironmentManager(void)
{
	OGRE_DELETE mTerrainManager;
}


void EnvironmentManager::Update(const Ogre::FrameEvent& iEvent)
{
	if(mTerrainManager)
		mTerrainManager->Update(iEvent);
}

}